package cn.hexcloud.dga.governance.service.impl;

import cn.hexcloud.dga.governance.bean.GovernanceAssessTable;
import cn.hexcloud.dga.governance.bean.GovernanceType;
import cn.hexcloud.dga.governance.mapper.GovernanceAssessTableMapper;
import cn.hexcloud.dga.governance.service.GovernanceAssessTableService;
import cn.hexcloud.dga.governance.service.GovernanceTypeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author jiangdan7
 * @since 2023-09-11
 */
@Service
@DS("dga")
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {

    @Autowired
    GovernanceTypeService governanceTypeService;

    @Override
    public void calcGovernanceAssessTable(String assessDate) {
        //1 汇总明细取得各个治理类型的评分，以及关联到各个表的大小和全局大小用于核算表权重
        List<GovernanceAssessTable> governanceAssessTables = baseMapper.calcScoreByDetail(assessDate);
        //2 后续还需要计算: 根据type权重计算  5维总分数
        List<GovernanceType> governanceTypes = governanceTypeService.list();
        //2.1 取出各个类型的系数
        HashMap<String, BigDecimal> governanceTypeMap = new HashMap<>(governanceTypes.size());
        BigDecimal sumWeight = BigDecimal.ZERO;
        for (GovernanceType governanceType : governanceTypes) {
            governanceTypeMap.put(governanceType.getTypeCode(),governanceType.getTypeWeight());
            sumWeight = sumWeight.add(governanceType.getTypeWeight());
        }
        //计算5维总分
        for (GovernanceAssessTable governanceAssessTable : governanceAssessTables) {
            BigDecimal specScore = governanceAssessTable.getScoreSpecAvg().multiply(governanceTypeMap.get("SPEC"));
            BigDecimal storageScore = governanceAssessTable.getScoreStorageAvg().multiply(governanceTypeMap.get("STORAGE"));
            BigDecimal calcScore = governanceAssessTable.getScoreCalcAvg().multiply(governanceTypeMap.get("CALC"));
            BigDecimal qualityScore = governanceAssessTable.getScoreQualityAvg().multiply(governanceTypeMap.get("QUALITY"));
            BigDecimal securityScore = governanceAssessTable.getScoreSpecAvg().multiply(governanceTypeMap.get("SECURITY"));
            BigDecimal allAvgScore = specScore.add(storageScore).add(calcScore).add(qualityScore).add(securityScore).divide(sumWeight,2,RoundingMode.HALF_UP);
            governanceAssessTable.setScoreOnTypeWeight(allAvgScore);
        }
        //保存
        saveBatch(governanceAssessTables,1000);
    }
}
